#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "sec_logger.h"


static int
createInitFile(char* fname)
{
	FILE* f;
	if((f=fopen(fname,"wb"))==NULL) 
		return -1;
	fclose(f);
	return 1;
}

int sec_logAction(const char * info)
{
	int ans;
	char pwd[10];
	char* newfile;
		
	strcpy(pwd,"HOLA");
	
	//decrypt the file
	if( ( ans = Twofish_dec(2, 128, SEC_LOGFILE, pwd))==-1)/*MODE_CBC*/
	{
		//VERRR el archivo no existe!!!
		if( createInitFile(LOGFILE)==-1)		
			return -1;
		printf("But NOW is created!!\n");
	}
	if((newfile=malloc(strlen(LOGFILE)+5))==NULL)
		return -1;
		
	sprintf(newfile,"%s.new",LOGFILE);

	rename(newfile,LOGFILE);

	//append new log
	logAction(info);

	//encrypt the file
	if( (ans=Twofish_enc(2, 128, LOGFILE, pwd))==-1)
		return -1;
	
	//remove intermediate file
	remove(LOGFILE);
		
	return ans;
}

